package com.luck.easyfoodbatch.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;


public class NumberUtils {

	// 返回单位是米
	private final static double PI = 3.14159265358979323; // 圆周率
	private final static double ER = 6371229; // 地球的半径
	private static final double EARTH_RADIUS = 6378137.0;

	/**
	 * 格式化double类型的数据，是结果保留两位小数
	 * @param value
	 * @return
	 */
	public static String format(double value){
		//对double类型的数据进行格式化的类
		return format(value,"######0.00");
	}
	
	/**
	 * 格式化double类型的数据
	 * @param value
	 * @return
	 */
	public static String format(double value, String format){
		//对double类型的数据进行格式化的类
		DecimalFormat deFormat=new DecimalFormat(format);
		return deFormat.format(value);
	}

	/**
	 * 计算两个坐标间的距离,单位米
	 * @param longt1
	 * @param lat1
	 * @param longt2
	 * @param lat2
	 * @return
	 */
	public static double calcDistance(double longt1, double lat1, double longt2,double lat2) {
        double x, y, distance;
        x = (longt2 - longt1) * PI * ER
                * Math.cos(((lat1 + lat2) / 2) * PI / 180) / 180;
        y = (lat2 - lat1) * PI * ER / 180;
        distance = Math.hypot(x, y);
        return distance;
    }
	
	/**
	 *   提供精确的加法运算。
	 *   @param   v1   被加数
	 *   @param   v2   加数
	 *   @return   两个参数的和
	 */

	public   static   double   add(double   v1,double   v2){
		BigDecimal b1   =   new BigDecimal(Double.toString(v1));
		BigDecimal b2   =   new BigDecimal(Double.toString(v2));
		return   b1.add(b2).doubleValue();
	}

	/**
	 *   提供精确的减法运算。
	 *   @param   v1   被减数
	 *   @param   v2   减数
	 *   @return   两个参数的差
	 */

	public   static   double   sub(double   v1,double   v2){
		BigDecimal b1   =   new BigDecimal(Double.toString(v1));
		BigDecimal b2   =   new BigDecimal(Double.toString(v2));
		return   b1.subtract(b2).doubleValue();
	}

	/**
	 *   提供精确的乘法运算。
	 *   @param   v1   被乘数
	 *   @param   v2   乘数
	 *   @return   两个参数的积
	 */

	public   static   double   mul(double   v1,double   v2){
		BigDecimal b1   =   new BigDecimal(Double.toString(v1));
		BigDecimal b2   =   new BigDecimal(Double.toString(v2));
		double result_value = b1.multiply(b2).doubleValue();
		String str = String.valueOf(result_value);
		int index = str.indexOf(".");
		String temp = str.substring(index + 1);
		if(temp.length()>=3){
			str = str.substring(0,index+3);
			result_value = Double.parseDouble(str);
		}

		return   result_value;
	}

	public static double getDistance(double longitude1, double latitude1,
									 double longitude2, double latitude2) {
		double Lat1 = rad(latitude1);
		double Lat2 = rad(latitude2);
		double a = Lat1 - Lat2;
		double b = rad(longitude1) - rad(longitude2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(Lat1) * Math.cos(Lat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	public static String millisToString(long millis) {
		boolean negative = millis < 0;
		millis = Math.abs(millis);

		millis /= 1000;
		int sec = (int) (millis % 60);
		millis /= 60;
		int min = (int) (millis % 60);
		millis /= 60;
		int hours = (int) millis;

		String time;
		DecimalFormat format = (DecimalFormat) NumberFormat
				.getInstance(Locale.US);
		format.applyPattern("00");
		if (millis > 0) {
			time = (negative ? "-" : "")
					+ (hours == 0 ? 00 : hours < 10 ? "0" + hours : hours)
					+ ":" + (min == 0 ? 00 : min < 10 ? "0" + min : min) + ":"
					+ (sec == 0 ? 00 : sec < 10 ? "0" + sec : sec);
		} else {
			time = (negative ? "-" : "") + min + ":" + format.format(sec);
		}
		return time;
	}
	
}
